/**
 * 新建关系
 */
function newConn(){
	try{
		$("#mappingFm").form('clear') ;
		$("#mappingDlg").dialog('open').dialog('setTitle','新建关系') ;
		tableInfo.actionType = "NEW" ;
	}catch(e){alert(e.message)}
}

/**
 * 修改关系
 */
function editConn(){
	try{
		var row = $("#mappingTable").datagrid('getSelected');
		if(row){
				$("#mappingFm").form('load',row) ;
				//初始化报送选项
				$("#mappingStatItemCode").combogrid('setValue',row.statItemCode) ;
				$("#mappingStatItemCode").combogrid('setText',row.statItemName) ;
				//初始化核心选项
				$("#mappingPrpItemCode").combogrid('setValue',row.prpItemCode) ;
				$("#mappingPrpItemCode").combogrid('setText',row.prpItemName) ;
				//打开选择框
				$("#mappingDlg").dialog('open').dialog('setTitle','修改关系') ;
				//更新状态为编辑
				tableInfo.actionType = "EDIT" ;
		}
	}catch(e){alert(e.message)}
}

/**
 * 删除关系
 */
function delConn(){
	var row = $("#mappingTable").datagrid('getSelected');
	if(row){
		$.messager.confirm('Confirm','你真的要删除这条记录吗',function(res){
			if(res){//如果选择了是
				var sql = "delete from " + tableInfo.mappingTableName
						+ " where statItemCode = '" + row.statItemCode
						+ "' and prpitemCode='" + row.prpItemCode + "'";
				//ajax调用执行SQL
				excuteUpdate(sql, function(){
					alert('删除成功!'+sql) ;
					$("#mappingTable").datagrid('reload');
				}) ;
			}
		});//end confirm method 
	}//end select if 
}

/**
 * 保存或者新增对应关系
 */
function mappingSave(){
	var sucFunc = null ;
	var sql = "" ;//操作数据库的SQL
	var checkSql= "" ;//检查数据库中是否已经有值的SQL
	checkSql = "select * from " + tableInfo.mappingTableName
			+ " where statItemCode = '"
			+ $("#mappingStatItemCode").combogrid('getValue')
			+ "' and prpItemCode = '"
			+ $("#mappingPrpItemCode").combogrid('getValue') + "' ";
	//根据更新或者新建组建不同的SQL
	if(tableInfo.actionType == "EDIT" ){//edit
		var row = $("#mappingTable").datagrid('getSelected');
		sql = "update " + tableInfo.mappingTableName
				+ " set statItemCode = '"
				+ $("#mappingStatItemCode").combogrid('getValue')
				+ "' , prpItemCode = '"
				+ $("#mappingPrpItemCode").combogrid('getValue')
				+ "' where statItemCode = '" + row.statItemCode
				+ "' and prpItemCode = '" + row.prpItemCode + "'";
	}else {//new
		sql = "insert into " + tableInfo.mappingTableName
				+ " (statItemCode,prpItemCode) values ('"
				+ $("#mappingStatItemCode").combogrid('getValue') + "','"
				+ $("#mappingPrpItemCode").combogrid('getValue') + "')";
	}
	
	sucFunc = function(){
		alert('数据库中已经存在这条记录!') ;
	}
	var failFun = function(){
		//远程执行SQL
		excuteUpdate(sql, function(){
			alert(sql) ;
			$("#mappingDlg").dialog('close') ;
			$("#mappingTable").datagrid('reload') ;
		});
	}//end failFun method
	
	excuteSelect(checkSql,sucFunc , failFun);
}//end method